package controllers

import (
  "github.com/gin-gonic/gin"
  "log"
  "rongyy/src/models"
  "rongyy/src/server"
  "rongyy/src/service"
  "strconv"
)

type AdminController struct {
  *service.AdminService
}

func NewAdminController() *AdminController {
  return &AdminController{}
}

func (sc *AdminController) Test(ctx *gin.Context) string {
  log.Println(465456465)
  return "bosuai"
}

//func (qc *SignInController) query(c *gin.Context) server.IModel {
//  limit := c.Query("limit")
//  limitInt, err := strconv.Atoi(limit)
//  offset := c.Query("offset")
//  offsetInt, err := strconv.Atoi(offset)
//  json := &models.DoctorInfo{}
//  err = c.ShouldBindJSON(json)
//  if err!= nil {
//
//  }
//  data, total, err := qc.Query(limitInt, offsetInt)
//  return &models.DataModel{IsSuccess: true, Msg: "提交成功", Data: map[string]interface{}{
//    "list": data,
//    "total": total}}
//}
func (qc *AdminController) query(c *gin.Context) server.IModel {
  limit := c.Query("limit")
  limitInt, err := strconv.Atoi(limit)
  offset := c.Query("offset")
  offsetInt, err := strconv.Atoi(offset)
  json := &models.DoctorModel{}
  err = c.ShouldBindJSON(json)
  if err!= nil {
    return &models.DataModel{IsSuccess: false, Msg: "参数错误"}
  }
  data, total, err := qc.Query(limitInt, offsetInt)
  return &models.DataModel{IsSuccess: true, Msg: "查询成功", Data: map[string]interface{}{
    "list": data,
    "total": total}}
}


func (sc *AdminController) signIn(c *gin.Context) server.IModel {
  req := &models.AdminModel{}
  err := c.ShouldBindJSON(req)
  if err!= nil {
    return &models.DataModel{IsSuccess: false, Msg: "参数错误"}
  }
  data, err := sc.SignIn(req)
  if err!= nil {
    return &models.DataModel{IsSuccess: false, Msg: err.Error()}
  }
  data.Password = ""
  return &models.DataModel{IsSuccess: true, Msg: "登录成功", Data: data}
}

func (sc *AdminController) add(c *gin.Context) server.IModel {
  req := &models.AdminModel{}
  err := c.ShouldBindJSON(req)
  if err!= nil {
    return &models.DataModel{IsSuccess: false, Msg: "参数错误"}
  }
  err = sc.Add(req)
  if err!= nil {
    return &models.DataModel{IsSuccess: false, Msg: err.Error()}
  }
  return &models.DataModel{IsSuccess: true, Msg: "添加成功"}
}


func (qc *AdminController) changePass(c *gin.Context) server.IModel {
  json := &models.AdminPassReq{}
  err := c.ShouldBindJSON(json)
  if err!= nil {
    return &models.DataModel{IsSuccess: false, Msg: err.Error()}
  }

  if len(json.NewPassword) > 8  {
    return &models.DataModel{IsSuccess: false, Msg: "新密码不得超过8位"}
  }

  if json.OldPassword ==  json.NewPassword  {
    return &models.DataModel{IsSuccess: false, Msg: "新密码不能与旧密码相同"}
  }
  err = qc.ChangePass(json)
  if err!= nil {
    return &models.DataModel{IsSuccess: false, Msg: err.Error()}
  }
  return &models.DataModel{IsSuccess: true, Msg: "修改成功"}
}
//

func (this *AdminController) RouterHandler(server *server.Server) {
  server.Handle("POST", "/add", this.add)
  //server.Handle("POST", "/relation/create", this.createRelationNew)
  server.Handle("POST", "/changePass", this.changePass)
  server.Handle("POST", "/signIn", this.signIn)
  server.Handle("POST", "/query", this.query)
  //server.Handle("POST", "/update", this.update)
}

